package com.lanlan.config;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.StringUtils;

import com.zaxxer.hikari.HikariDataSource;

/**
 * 
 * @ClassName: DataSourceConfig
 * @Description: 数据源
 * @author XGJ
 * @date 2022-10-13 10:20:37
 */
@Configuration
public class DataSourceConfig {

    /**
     * 
     * @Title: userDataSourceProperties
     * @Description: 读取 spring.datasource.user 配置到DataSourceProperties对象
     * @return
     * @author XGJ
     * @date 2022-10-19 05:40:58
     */
    @Primary
    @Bean(name = "userDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.user")
    public DataSourceProperties userDataSourceProperties() {
	return new DataSourceProperties();
    }

    /**
     * 
     * @Title: userDataSource
     * @Description: 读取 spring.datasource.user 配置到HikariDataSource 对象
     * @return
     * @author XGJ
     * @date 2022-10-19 05:41:05
     */
    @Bean(name = "userDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.user.hikari")
    public DataSource userDataSource() {
	// <1.1> 获得 DataSourceProperties 对象
	DataSourceProperties properties = this.userDataSourceProperties();
	// <1.2> 创建 HikariDataSource 对象
	return createHikariDataSource(properties);
    }

    /**
     * 
     * @Title: vendorDataSourceProperties
     * @Description: 读取 spring.datasource.vendor 配置到DataSourceProperties对象
     * @return
     * @author XGJ
     * @date 2022-10-19 05:40:58
     */
    @Primary
    @Bean(name = "vendorDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.vendor")
    public DataSourceProperties vendorDataSourceProperties() {
	return new DataSourceProperties();
    }

    /**
     * 
     * @Title: vendorDataSource
     * @Description: 读取 spring.datasource.vendor 配置到HikariDataSource 对象
     * @return
     * @author XGJ
     * @date 2022-10-19 05:41:05
     */
    @Bean(name = "vendorDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.vendor.hikari")
    public DataSource vendorDataSource() {
	// <1.1> 获得 DataSourceProperties 对象
	DataSourceProperties properties = this.vendorDataSourceProperties();
	// <1.2> 创建 HikariDataSource 对象
	return createHikariDataSource(properties);
    }

    /**
     * 
     * @Title: createHikariDataSource
     * @Description: 创建 HikariDataSource 对象
     * @param properties
     * @return
     * @author XGJ
     * @date 2022-10-19 05:44:37
     */
    private static HikariDataSource createHikariDataSource(DataSourceProperties properties) {
	// 创建 HikariDataSource 对象
	HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
	// 设置线程池名
	if (StringUtils.hasText(properties.getName())) {
	    dataSource.setPoolName(properties.getName());
	}
	return dataSource;
    }

}
